package cn.tedu.egaddress0805.controller;

import cn.tedu.egaddress0805.mapper.AddressMapper;
import cn.tedu.egaddress0805.pojo.dto.AddressAddParam;
import cn.tedu.egaddress0805.pojo.entity.Address;
import cn.tedu.egaddress0805.pojo.vo.AddressInfoVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/v1/address/")
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("add")
    public String addAddress(@RequestBody AddressAddParam  addressAddParam) {
        System.out.println("addressAddParam:" + addressAddParam);
        Address address = new Address();

        BeanUtils.copyProperties(addressAddParam, address);

        int rows = addressMapper.insertAddress(address);
        System.out.println("地址添加成功");
        return "地址添加成功！";
    }

    @GetMapping("list")
    public List<AddressInfoVO> selectAddressList() {
        List<AddressInfoVO> list = addressMapper.selectAddressList();
        System.out.println("地址列表查询成功");
        return list;
    }

    @PostMapping("del/{addressId}")
    public String deleteAddress(@PathVariable Integer addressId){
        int rows = addressMapper.deleteAddress(addressId);
        return rows > 0 ? "删除地址成功" : "删除地址失败";
    }

}































